<div style='color:black;'>
<?php
	include_once("../MasterPages/header.php");
	include_once("../phpServices/Classes/PHPExcel.php");
	
	if(isset($_FILES["uploadFileExcel"]))
	{		
		// require_once 'PHPExcel/IOFactory.php';

		$excelFile = $_FILES["uploadFileExcel"]["tmp_name"];
		$objReader = PHPExcel_IOFactory::createReader('Excel2007');
		$objPHPExcel = $objReader->load($excelFile);

		//Itrating through all the sheets in the excel workbook and storing the array data
		foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
			$arrayData[$worksheet->getTitle()] = $worksheet->toArray();
		}

		// var_dump($arrayData["Sheet1"]);		
		$index = 0;
		$lstUserProfileUploadDTO = array();
		$UserService = new UserService();
		$PrefixService = new PrefixService();
		$lstPrefix = $PrefixService->GetPrefix();
		$lstUserSession = $UserService->GetUserProfileByUserCodeAndName();
		$lstUserCode = array();
		foreach($arrayData["Sheet1"] as $array)
		{
			if($index!=0){
				$UserProfileUploadDTO = new UserProfileUploadDTO();
				$UserProfileUploadDTO->UserCode =  $array[0];
				$UserProfileUploadDTO->Prefix =  iconv("UTF-8","TIS-620",$array[1]);
				$UserProfileUploadDTO->Role = iconv("UTF-8","TIS-620",$array[2]);
				$UserProfileUploadDTO->Password = $array[3];
				$UserProfileUploadDTO->Name = iconv("UTF-8","TIS-620",$array[4]);
				$UserProfileUploadDTO->Surname = iconv("UTF-8","TIS-620",$array[5]);
				$UserProfileUploadDTO->Email = $array[6];
				array_push($lstUserCode,$UserProfileUploadDTO->UserCode);
				array_push($lstUserProfileUploadDTO,$UserProfileUploadDTO);
			}
			$index++;
		}
		$lstError = array();
		$index = 0;
		$UserService = new UserService();
		$PrefixService = new PrefixService();
		$RoleService = new RoleService();
		// echo "<table border=1>";
		foreach($lstUserProfileUploadDTO as $key)
		{		
			// echo "<tr>";
			// echo "<td>" . $key->UserCode . "</td>";
			// echo "<td>" . $key->Password . "</td>";
			// echo "<td>" . $key->Email . "</td>";
			// echo "<td>" . $key->Name . "</td>";
			// echo "<td>" . $key->Surname . "</td>";
			// echo "<td>" . $key->Prefix . "</td>";
			// echo "<td>" . $key->Role . "</td>";
			// echo "</tr>";
			$lstError[$index] = "";
			if($key->UserCode!=""){				
				$UserProfile = $UserService->GetUserProfileByUserCode($key->UserCode);
				if(strlen($key->UserCode) > 20){ $lstError[$index] .= GetErrorMessage("รหัสผู้ใช้จะต้องน้อยกว่า 20 ตัวอักษร"); }
				if(count(array_keys($lstUserCode, $key->UserCode)) > 1){ $lstError[$index] .= GetErrorMessage("รหัสผู้ใช้ซ้ำใน Excel"); }
				if($UserProfile->UserCode!=""){ $lstError[$index] .= GetErrorMessage("รหัสผู้ใช้ซ้ำในระบบ"); }
			}else{ $lstError[$index] .= GetErrorMessage("กรุณากรอกข้อมูลผู้ใช้"); }
			
			if($key->Password!=""){
				if(strlen($key->Password) > 50){ $lstError[$index] .= GetErrorMessage("รหัสผ่านจะต้องน้อยกว่า 50 ตัวอักษร"); }
			}else{ $lstError[$index] .= GetErrorMessage("กรุณากรอกรหัสผ่าน"); }
			
			if($key->Email!=""){
				if(strlen($key->Email) > 50){ $lstError[$index] .= GetErrorMessage("อีเมล์จะต้องน้อยกว่า 50 ตัวอักษร"); }
				if(!filter_var($key->Email, FILTER_VALIDATE_EMAIL)) { $lstError[$index] .= GetErrorMessage("รูปแบบอีเมล์ไม่ถูกต้อง");}
			}else{ $lstError[$index] .= GetErrorMessage("กรุณากรอกอีเมล์"); }
			
			if($key->Name!=""){
				if(strlen($key->Name) > 100){ $lstError[$index] .= GetErrorMessage("ชื่อผู้ใช้จะต้องน้อยกว่า 100 ตัวอักษร"); }
			}else{ $lstError[$index] .= GetErrorMessage("กรุณากรอกชื่อ"); }
			
			if($key->Surname!=""){
				if(strlen($key->Surname) > 100){ $lstError[$index] .= GetErrorMessage("นามสกุลผู้ใช้จะต้องน้อยกว่า 100 ตัวอักษร"); }
			}else{ $lstError[$index] .= GetErrorMessage("กรุณากรอกนามสกุล"); }			

			if($key->Prefix!=""){				
				$PrefixID = $PrefixService->GetPrefixIDByPrefixName($key->Prefix);				
				if($PrefixID==null || $PrefixID==""){ $lstError[$index] .= GetErrorMessage("ไม่พบคำนำหน้านี้ในระบบ"); }
				else{
					$key->Prefix = $PrefixID;
				}
			}else{ $lstError[$index] .= GetErrorMessage("กรุณากรอกคำนำหน้าชื่อ"); }			
			 
			if($key->Role!=""){				
				$RoleID = $RoleService->GetRoleIDByRoleName($key->Role);				
				if($RoleID==""){ $lstError[$index] .= GetErrorMessage("ไม่พบสิทธิ์นี้ในระบบ"); }
				else{
					$key->Role = $RoleID;
				}
			}else{ $lstError[$index] .= GetErrorMessage("กรุณากรอกสิทธิ์ของผู้ใช้"); }
			 
			$index++;
		}
		// echo "</table>";
		
		//var_dump($lstError);
		$errorMsg = "<ul class='lstError'>";
		$errorFound = false;
		for($i=0;$i<count($lstError);$i++)
		{
			if(isset($lstError[$i]) && $lstError[$i]!="")
			{
				$errorFound = true;
				$index = ($i+1);
				$errorMsg .= "<li>Error #$index<ul>" . $lstError[$i] . "</ul></li>";
			}
		}
		$errorMsg .= "</ul>";
		
		if(!$errorFound)
		{
			foreach($lstUserProfileUploadDTO as $key)
			{
				$UserProfile = new UserProfile();
				$UserProfile->UserCode = $key->UserCode;
				$UserProfile->Password = $key->Password;
				$UserProfile->Email = $key->Email;
				$UserProfile->Name = $key->Name;
				$UserProfile->Surname = $key->Surname;
				$UserProfile->PrefixID = $key->Prefix;
				$UserService->InsertUserProfile($UserProfile,$key->Role);
			}
			Header("location:index.php?page=admin-manage-profile.php");
		}else{
			$SessionID = uniqid();
			$_SESSION[$SessionID] = $errorMsg;
			Header("location:index.php?page=uploadExcelFile.php&SessionID=$SessionID");
		}
	}
	
	function GetErrorMessage($msg){
		return "<li>" . iconv("UTF-8","TIS-620",$msg) . "</li>";
	}
?>
<form method='POST' action='uploadExcelFile.php' enctype="multipart/form-data">
		<input type='file' name='uploadFileExcel' id='uploadFileExcel'/>
		<br>
		<input type='submit' value='Upload' />
		<input type='button' value='Download Sample' onclick='GoTo("../Resources/ExampleFiles/CourseSchedule.xlsx");' />
</form>
</div>
<?php
	if(isset($_GET["SessionID"]))
	{
		$SessionID = $_GET["SessionID"];
		echo $_SESSION[$SessionID];
	}
?>